package com.innovation.chengzhangriji.ui.widget;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.ui.model.UIPublicMilestone;

import org.lucasr.twowayview.widget.SpannableGridLayoutManager;

import java.util.ArrayList;

public class PublicMilestoneAdapter extends RecyclerView.Adapter<BasePublicMilestoneAdapterViewHolder> {

    private ArrayList<UIPublicMilestone> mList;

    private int mSelectPosition = -1;

    @Override
    public BasePublicMilestoneAdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view;
        BasePublicMilestoneAdapterViewHolder viewHolder;
        switch (viewType) {
            case UIPublicMilestone.BASE_TYPE_TITLE:
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_public_milestone_title, viewGroup, false);
                viewHolder = new PublicMilestoneTitleAdapterViewHolder(view);
                return viewHolder;
            case UIPublicMilestone.BASE_TYPE_VALUE:
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_public_milestone_value, viewGroup, false);
                viewHolder = new PublicMilestoneValueAdapterViewHolder(view);
                return viewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(BasePublicMilestoneAdapterViewHolder viewHolder, int position) {

        final View itemView = viewHolder.itemView;
        final SpannableGridLayoutManager.LayoutParams lp =
                (SpannableGridLayoutManager.LayoutParams) itemView.getLayoutParams();
        int colSpan = 1;
        int rowSpan = 1;
        switch (getItemViewType(position)) {
            case UIPublicMilestone.BASE_TYPE_TITLE:
                colSpan = 30;
                rowSpan = 5;
                switch (mList.get(position).getType()) {
                    case UIPublicMilestone.TYPE_1:
                        ((PublicMilestoneTitleAdapterViewHolder) viewHolder).setTitle("备孕");
                        break;
                    case UIPublicMilestone.TYPE_2:
                        ((PublicMilestoneTitleAdapterViewHolder) viewHolder).setTitle("孕期");
                        break;
                    case UIPublicMilestone.TYPE_3:
                        ((PublicMilestoneTitleAdapterViewHolder) viewHolder).setTitle("成长期");
                        break;
                }
                break;
            case UIPublicMilestone.BASE_TYPE_VALUE:
                colSpan = 6;
                rowSpan = 5;
                if (mList.get(position).getValue() != null) {
                    ((PublicMilestoneValueAdapterViewHolder) viewHolder).setMilestone(mList.get(position).getValue());
                    ((PublicMilestoneValueAdapterViewHolder) viewHolder).select(mList.get(position).isSelect());
                }
                if (mList.get(position).getType() != UIPublicMilestone.TYPE_1 && mList.get(position).getValue() != null) {
                    ((PublicMilestoneValueAdapterViewHolder) viewHolder).setMilestoneExtra(mList.get(position).getExtra());
                    ((PublicMilestoneValueAdapterViewHolder) viewHolder).showExtra(true);
                } else {
                    ((PublicMilestoneValueAdapterViewHolder) viewHolder).showExtra(false);
                }
                break;
        }

        if (lp.colSpan != colSpan || lp.rowSpan != rowSpan) {
            lp.colSpan = colSpan;
            lp.rowSpan = rowSpan;
            itemView.setLayoutParams(lp);
        }
    }

    @Override
    public int getItemCount() {
        if (mList != null && mList.size() > 0) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (mList != null) {
            return mList.get(position).getBaseType();
        }
        return -1;
    }

    public void setList(ArrayList<UIPublicMilestone> list) {
        mList = list;
        notifyDataSetChanged();
    }

    public boolean onItemClick(int position) {
        if (mList.get(position).getBaseType() == UIPublicMilestone.BASE_TYPE_TITLE) {
            return false;
        }
        if (TextUtils.isEmpty(mList.get(position).getValue())) {
            return false;
        }
        if (mSelectPosition == position) {
            return false;
        }

        int last = mSelectPosition;
        mSelectPosition = position;
        if (last != -1) {
            mList.get(last).setSelect(false);
            notifyItemChanged(last);
        }
        mList.get(mSelectPosition).setSelect(true);
        notifyItemChanged(mSelectPosition);
        return true;
    }

    public UIPublicMilestone getSelect() {
        if (mSelectPosition != -1) {
            return mList.get(mSelectPosition);
        }
        return null;
    }
}
